$(document).ready(function () {
	//获取账户信息
	//get_bank_info();
	function get_bank_info() {
		var form = $('#user_bank_form');
		$.ajax({
			url: '/index/recharge',
			type:'POST',
			dataType: 'json',
			data: form.serialize(),
			success:function (data) {
				if( data.code == 1 ){
					form.find('input[name="bank_uname"]').val( data.data.bank_uname );
					form.find('input[name="bank_card"]').val( data.data.bank_card );
					form.find('input[name="bank_name"]').val( data.data.bank_name );
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//选择充值类型、
	select_recharge_status();
	function select_recharge_status() {
		var form = $('#recharge_form');
		$('#select_status').delegate('.text-center','click',function () {
			$('#select_status').find('.active').removeClass('active');
			$(this).find('div').addClass('active');

			var status = '';//定义充值类型 1：普通  2：白金  3：钻石  5:黄金  6：口腔
			if( $(this).find('div').text() == '普通' ){
				status = '1'
			}
			if( $(this).find('div').text() == '黄金' ){
				status = '5'
			}
			if( $(this).find('div').text() == '白金' ){
				status = '2'
			}
			if( $(this).find('div').text() == '钻石' ){
				status = '3'
			}
			if( $(this).find('div').text() == '口腔' ){
				status = '6'
			}
			form.find('input[name="score_type"]').val( status );
		})
	}

	//选择充值金额、
	select_recharge_money();
	function select_recharge_money() {
		var form = $('#recharge_form');
		var input = $('#recharge_money_input');//输入充值金额
		$('#select_recharge_moeny').delegate('.text-center','click',function () {
			var money = '';
			$('#select_recharge_moeny').find('.active').removeClass('active');
			$(this).find('div').addClass('active');

			input.val('');

			if( $(this).find('div').text() == '1万' ){
				money = '10000'
			}
			if( $(this).find('div').text() == '5万' ){
				money = '50000'
			}
			if( $(this).find('div').text() == '10万' ){
				money = '100000'
			}
			if( $(this).find('div').text() == '20万' ){
				money = '200000'
			}
			form.find('input[name="recharge_money"]').val( money );
		})
	}
	
	//输入充值金额
	input_recharge_money();
	function input_recharge_money() {
		var form = $('#recharge_form');//form表单
		var input = $('#recharge_money_input');//输入充值金额
		var select_money = $('#select_recharge_moeny');//选择充值金额

		//聚焦
		input.on('focus',function () {
			select_money.find('.active').removeClass('active');
			if ( $(this).val() ){
				form.find('input[name="recharge_money"]').val( $(this).val() );
			}
		});

		//输入  限制只能输入数字
		input.on('keyup',function () {
			$(this).val( $(this).val().replace(/[^\d]/g,'') );
		});

		//失去焦点
		input.on('blur',function () {
			if ( $(this).val() ){
				form.find('input[name="recharge_money"]').val( $(this).val() );
			}
		});

	}
	
	//确认充值
	sure_recharge();
	function sure_recharge() {
		$('#recharge_btn').on('click',function () {
			var form = $('#recharge_form');//form表单
			var input = $('#recharge_money_input');//输入充值金额
			var select_money = $('#select_recharge_moeny');//选择充值金额

			if( select_money.find('.active').length > 0 || input.val() ){
				$.ajax({
					url: '/index/recharge_order',
					type:'POST',
					dataType: 'json',
					data: form.serialize(),
					success:function (data) {
						form.find('input[name="score_type"]').val( '1' );
						form.find('input[name="recharge_money"]').val( '10000' );
						input.val('');
						if(data.code == 1){
							$('#myModal').find('.modal-body').text('提交成功！');
							$('#myModal').modal();
							$('#myModal').on('hide.bs.modal',function () {
								window.location = '/index/user_recharge_record';
							});
						}
					},
					error:function (err) {
						console.log(err);
					}
				});
			}else {
				$('#myModal').find('.modal-body').text('请您选择或输入充值金额');
				$('#myModal').modal();
			}

		});
	}

	//返回上一页
	function go_back() {
		history.go(-1);
	}

	//点击返回键  返回上一页
	click_back_btn();
	function click_back_btn() {
		$('#go_back').on('click',function () {
			go_back();//返回上一页
		});
	}

});